package org.dynobjects.core;

import java.util.ArrayList;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.jci.compilers.CompilationResult;
import org.apache.commons.jci.compilers.EclipseJavaCompiler;
import org.apache.commons.jci.compilers.JavaCompiler;
import org.apache.commons.jci.problems.CompilationProblem;
import org.apache.commons.jci.readers.MemoryResourceReader;
import org.apache.commons.jci.readers.ResourceReader;
import org.apache.commons.jci.stores.MemoryResourceStore;
import org.apache.commons.jci.stores.ResourceStore;
import org.apache.commons.lang.StringUtils;

public class RuntimeCompiler {

	
	public static void compile(BeanCreator factory) throws Exception {
		final JavaCompiler compiler = new EclipseJavaCompiler();
		final ResourceStore store = new MemoryResourceStore();

		final ResourceReader reader = new MemoryResourceReader();
		Map map = factory.getSourcesContainer();
		if(map.size()==0){
			//per assicurarmi che i sorgenti vengano creati
			factory.createSourceCode();
		}
		Iterator keys = map.keySet().iterator();
		List sourcesName = new ArrayList();
		while (keys.hasNext()) {
			String srcName = (String) keys.next();// nome del file java
			// (comprensivo
			// dell'estensione)

			sourcesName.add(StringUtils.replaceChars(srcName.substring(0,
					srcName.lastIndexOf(".")), ".", "/")
					+ ".java");
			String _classCode = (String) map.get(srcName);
			((MemoryResourceReader) reader).add(srcName, _classCode.getBytes());
		}
		String[] resources = new String[sourcesName.size()];
		sourcesName.toArray(resources);
		EPClassLoader classLoader = EPClassLoader.getInstance(ClassLoader.getSystemClassLoader());
		
		System.out.println("??????????????????????????????????????");
		System.out.println("??????????????????????????????????????");
		System.out.println("??????????????????????????????????????");
		System.out.println("ClassLoader: "+ClassLoader.getSystemClassLoader());
		CompilationResult compilationResult = compiler.compile(resources, reader, store,store.getClass().getClassLoader());
		CompilationProblem[] problems = compilationResult.getErrors();
		if(problems.length>0){
			System.out.println("===== Compilation errors ====");
			for (int i = 0; i < problems.length; i++) {
				CompilationProblem problem = problems[i];
				System.out.println("\t\t"+problem.getFileName());
				System.out.println("\t\t"+problem.getStartLine());
				System.out.println("\t\t"+problem.getMessage());
			}
		}
		for (int i = 0; i < resources.length; i++) {
			String string = resources[i];
			String _classResource = StringUtils.replace(string, ".java",
					".class");


			String qualifiedClassName = StringUtils.replace(_classResource,
					".class", "");
			qualifiedClassName = StringUtils.replaceChars(qualifiedClassName,
					"/", ".");
			Class c = classLoader.findClass(store
					.read(_classResource),qualifiedClassName);

		}
		
	}

	

}
